﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Model;
using BLL.Privilege;
using BLL.Privilege.Impl;
using BLL.Info;
using BLL.Info.Impl;
using GenericLibrariesToolKit;
using Utils;

public partial class Page_News_NewsList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            showNewsList();
            //发表新闻权限控制
            postNewsPrivilegeControl();
    }
    /// <summary>
    /// 发表新的新闻
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNewsListPostSubmit_Click(object sender, ImageClickEventArgs e)
    {
        //获取数据
        string newsTitle = txtPostNewsTitle.Text.Trim();
        string newsKeyWord = txtPostNewsKeywords.Text.Trim();
        string newsDetail = txtPostNewsDetails.Text.Trim();
        //封装数据
        News news = new News() 
        {

            NewsContent=newsDetail,
            NewsKey=newsKeyWord,
            NewsPosterId=WebUtil.GetIdentifier(Session["userId"]),
            NewsTitle=newsTitle,
        };
        //处理数据
        NewsBO newsBO = new NewsBOImpl();
        bool result=newsBO.Add(news);
        //清空文本框数据
        clearData();
        //刷新
        if(result)
            showNewsList();
    }
    /// <summary>
    /// 显示新闻列表
    /// </summary>
    public void showNewsList()
    {
        NewsBO newsBO=new NewsBOImpl();
        //设置order by 排序
        Dictionary<string, string> orderBy = new Dictionary<string, string>();
        orderBy.Add("NewsDate", "desc");

        DataSet datasetNews = newsBO.GetDataSet(-1,-1,null,orderBy);
        gvNewsList.DataSource = datasetNews;
        gvNewsList.DataBind();
    }

    /// <summary>
    ///对发表新闻进行权限控制
    /// </summary>
    public void postNewsPrivilegeControl()
    {
        //获取用户ID
        string userID = WebUtil.GetIdentifier(Session["userId"]);
        /*--test--
        RoleModelRight roleModeRight = new RoleModelRightImpl();
        roleModeRight.GetRoleRight(userID);
        --test--*/
        bool result = isPostNewsPrivilege(userID);
        //隐藏发表新闻DIV
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", GenerateScript(result));
    }

    /// <summary>
    /// 判断用户是否有发表新闻的权限
    /// </summary>
    /// <returns></returns>
    public bool isPostNewsPrivilege(string userID)
    {
        RoleRightBO roleRightBO=new RoleRightBOImpl();
        //如果用户为游客，返回false
        if (userID == null)
            return false;
        //获取发表新闻的权限
        bool result = roleRightBO.IsPrivilege(userID, "newsInfo", OperateType.add);
        return result;
    }
    /// <summary>
    /// 生成js脚本
    /// </summary>
    /// <param name="isShowLoginMaster"></param>
    /// <returns></returns>
    private string GenerateScript(bool isShowLoginMaster)
    {
        //存放script的容器
        StringBuilder script = new StringBuilder();
        script.Append("<script>");
        if (isShowLoginMaster)
        {
            script.Append("document.getElementById('PostNews').style.display='block';");
        }
        else
        {
            script.Append("document.getElementById('PostNews').style.display='none';");
        }
        script.Append("</script>");

        return script.ToString();
    }

    /// <summary>
    /// 分页处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvNewsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvNewsList.PageIndex = e.NewPageIndex;
        this.gvNewsList.DataBind();
    }
    //返回主页面
    protected void btnNewsBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script language=javascript>history.go(-2);</script>");
    }
    //清空页面
    private void clearData()
    {
        txtPostNewsTitle.Text = "";
         txtPostNewsKeywords.Text="";
         txtPostNewsDetails.Text="";
    }
}